//
//  QDSettingAndHelpController.m
//  QuickDoctor
//
//  Created by ycw on 2016/12/21.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDSettingAndHelpController.h"
#import "QDSettingAndHelpCell.h"
#import "QDMainViewController.h"
#import "QDSideslippingViewController.h"
#import "QDTicklingController.h"


static NSString *cellID = @"settingAndHelpCell";

@interface QDSettingAndHelpController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic) NSArray<NSString *> *array;

//侧滑控制器
@property (nonatomic, strong) JASidePanelController *panelController;

@end

@implementation QDSettingAndHelpController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"设置与帮助";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    
    [self.view addSubview:tableView];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    tableView.scrollEnabled = NO;
    
    [tableView registerClass:[QDSettingAndHelpCell class] forCellReuseIdentifier:cellID];
    
    self.array = @[@"意见反馈",@"加号符号说明",@"修改密码",@"注销用户"];
    
    
}


#pragma mark
#pragma mark - 实现数据源代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    QDSettingAndHelpCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.label.text = self.array[indexPath.row];
    
    return cell;
    
}

//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //取消选中的方法
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        
        QDTicklingController *tickling = [[QDTicklingController alloc] init];
        
        [self.navigationController pushViewController:tickling animated:YES];
        
    }
    
    if (indexPath.row == 3) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定要注销吗?" preferredStyle:UIAlertControllerStyleActionSheet];
        
        // 添加action
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        // 添加action到alertController
        [alertController addAction:cancelAction];
        
        //添加确定Action
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:KEYFORLOGIN];
            isLogin = NO;
            [[NSUserDefaults standardUserDefaults] setBool:isLogin forKey:KEYFORLOGIN];
            
            self.panelController = [[JASidePanelController alloc] init];
            
            QDSideslippingViewController *sidesVC = [[QDSideslippingViewController alloc] init];
            
            self.panelController.leftPanel = sidesVC;
            self.panelController.leftPanel.view.backgroundColor = [UIColor whiteColor];
            
            QDMainViewController *viewC = [QDMainViewController new];
            self.panelController.centerPanel = [[UINavigationController alloc] initWithRootViewController:viewC];
            
            sidesVC.tableView.tableHeaderView = [sidesVC createLoginHeaderView];

            [SVProgressHUD showSuccessWithStatus:@"注销成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            
            [self presentViewController:self.panelController animated:NO completion:nil];
            
        }];
        
        [alertController addAction:okAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
    
    
}

@end
